package com.magic.gre.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.entity.AnswerItemBean;
import com.noname.lib_base_java.adapter.CommonAdapter;
import com.noname.lib_base_java.adapter.ViewHolder;
import com.noname.lib_base_java.util.SelectHelper;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-08 19:26
 * 文件名: AnswerItemAdapter
 * 描述:
 */
public class AnswerItemAdapter extends CommonAdapter<AnswerItemBean> {

    private OnAnswerItemClickListener onAnswerItemClickListener;
    private String titile;

    public void setOnAnswerItemClickListener(OnAnswerItemClickListener onAnswerItemClickListener) {
        this.onAnswerItemClickListener = onAnswerItemClickListener;
    }

    public AnswerItemAdapter(Context mContext, List<AnswerItemBean> mData) {
        super(mContext, mData);
    }

    public void setTitile(String titile) {
        this.titile = titile;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(viewType, parent, false));
    }

    @Override
    protected void bindData(ViewHolder holder, final int position) {
        if (getItemViewType(position) == R.layout.item_answer_item_head) {
            if (onAnswerItemClickListener != null) {
                onAnswerItemClickListener.onTitleTv((TextView) holder.getView(R.id.words_tv));
            }
        } else {
            final AnswerItemBean bean = mData.get(position - 1);
            final TextView contentTv = holder.getView(R.id.content_tv);
            contentTv.setText(bean.getItemName());
            holder.itemView.setEnabled(!bean.isSelect());
            if (bean.getTag() == 0) {
                contentTv.setBackgroundResource(R.drawable.bg_answer_item_item_normal);
                contentTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_333333));
                contentTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.answer_defult_icon, 0);
            } else if (bean.getTag() == 1) {
                contentTv.setBackgroundResource(R.drawable.bg_answer_item_item_yes);
                contentTv.setTextColor(Color.parseColor("#1DC69B"));
                contentTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_answer_yes, 0);
            } else if (bean.getTag() == 2) {
                contentTv.setBackgroundResource(R.drawable.bg_answer_item_item_no);
                contentTv.setTextColor(Color.parseColor("#ED4A2F"));
                contentTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_answer_no, 0);
            }


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onAnswerItemClickListener != null) {
                        SelectHelper.selectAllSelectBeans(mData, true);
                        if (bean.getIsRight() == 1) {
                            bean.setTag(1);
                            notifyDataSetChanged();
                            onAnswerItemClickListener.onCheckYes();
                        } else {
                            bean.setTag(2);
                            notifyDataSetChanged();
                            onAnswerItemClickListener.onCheckNo(position);
                        }

                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return R.layout.item_answer_item_head;
        } else {
            return R.layout.item_answer_item;
        }
    }

    public interface OnAnswerItemClickListener {
        void onCheckYes();

        void onTitleTv(TextView textView);

        void onCheckNo(int position);
    }
}
